package com.sphd.education.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.sphd.education.EducationApplication;
import com.sphd.education.R;
import com.sphd.education.activitys.FeedBackActivity;
import com.sphd.education.activitys.HistoryActivity;
import com.sphd.education.activitys.LoginActivity;
import com.sphd.education.activitys.MyAddNameLessonActivity;
import com.sphd.education.activitys.MySelfActivity;
import com.sphd.education.activitys.RegisterActivity;
import com.sphd.education.activitys.SettingActivity;
import com.sphd.education.info.EducationIntentKey;
import com.sphd.education.info.MyInfo;
import com.sphd.education.info.Myclassify;
import com.sphd.education.interfaces.FragmentInterface;
import com.sphd.education.modle.EducationUrl;
import com.sphd.education.utils.CheckUpdate;
import com.sphd.education.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我
 * Created by Administrator on 2015/10/12.
 */
public class MineFragment extends Fragment {
    @Bind(R.id.my_collect_card)
    CardView myCollectCard;
    @Bind(R.id.my_apply_card)
    CardView myApplyCard;
    @Bind(R.id.my_history_card)
    CardView myHistoryCard;
    @Bind(R.id.my_setting_card)
    CardView mySettingCard;
    @Bind(R.id.my_feedback_card)
    CardView myFeedbackCard;
    @Bind(R.id.logon_button)
    Button logonButton;
    @Bind(R.id.register_button)
    Button registerButton;
    @Bind(R.id.user_img)
    ImageView userImg;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.my_qianming)
    TextView myQianming;
    @Bind(R.id.my_title)
    TextView myTitle;
    @Bind(R.id.my_selftitle_card)
    CardView mySelftitleCard;
    @Bind(R.id.my_changepassword_card)
    CardView myChangepasswordCard;
    @Bind(R.id.logon_register_card)
    CardView logonRegisterCard;
    private String Tag = "MineFragment";
    private FragmentInterface fragmentInterface;
    private View rootView;
    private Context context;
    private Intent intent;
    private MyInfo myInfo;//我的实体类存储设置的相关信息
    private Bundle bundle;

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static MineFragment newInstance(Bundle bundle) {
        MineFragment fragment = new MineFragment();

        fragment.setArguments(bundle);//传值判断是那个fragment
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EducationUrl.showLog("onCreate");
        context = getActivity();
        intent = new Intent();
        bundle = new Bundle();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        EducationUrl.showLog("onCreateView");
        rootView = inflater.inflate(R.layout.fragment_mine_layout, container, false);
        ButterKnife.bind(this, rootView);

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        myInfo = DataSupport.findLast(MyInfo.class);
        if (myInfo == null) {
            myInfo = new MyInfo();
            myInfo.setLogon_status(0);
            myInfo.save();
        }
        if (myInfo.getLogon_status() == 0) {
            myChangepasswordCard.setVisibility(View.GONE);
            mySelftitleCard.setVisibility(View.GONE);
            logonRegisterCard.setVisibility(View.VISIBLE);
            myTitle.setVisibility(View.VISIBLE);
        } else {
            myChangepasswordCard.setVisibility(View.VISIBLE);
            mySelftitleCard.setVisibility(View.VISIBLE);
            logonRegisterCard.setVisibility(View.GONE);
            myTitle.setVisibility(View.GONE);
            userName.setText(myInfo.getName());
            if (myInfo.getName().equals("")) {
                userName.setText("未填写");
            }
            if (!TextUtils.isEmpty(myInfo.getDescription())){
                myQianming.setText(myInfo.getDescription());
            }
        }
        ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + myInfo.getUserimgurl(),
                userImg, EducationUrl.setImg(R.mipmap.touxiangweishangchuan));
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();

        EducationUrl.showLog("onDestroyView");
        ButterKnife.unbind(this);
    }

    // 接收activity传递过来的接口子类对象
    public void setCallBack(FragmentInterface fragmentInterface) {
        this.fragmentInterface = fragmentInterface;
    }

    //我的收藏
    @OnClick(R.id.my_collect_card)
    public void setOnClickCollect() {
        if (myInfo == null || myInfo.getUserid() == 0) {
            EducationUrl.showToast(context, "尚未登录");
            intent.setClass(context, LoginActivity.class);
            startActivityForResult(intent,1);
        }else {
            bundle.putString(EducationIntentKey.FLAGACTIVITY, EducationIntentKey.COLLECT);
            intent.putExtra("mine", bundle);
            intent.setClass(context, HistoryActivity.class);
            startActivity(intent);
        }
    }
    //我的报名
    @OnClick(R.id.my_apply_card)
    public void setOnClickApply() {
        if (myInfo == null || myInfo.getUserid() == 0) {
            EducationUrl.showToast(context, "尚未登录");
            intent.setClass(context, LoginActivity.class);
            startActivityForResult(intent,1);
        }else {
            bundle.putSerializable("myinfo", myInfo);
            intent.putExtra("mine", bundle);//需修改统一字段
            intent.setClass(context, MyAddNameLessonActivity.class);
            startActivity(intent);
        }
    }
    //历史浏览
    @OnClick(R.id.my_history_card)
    public void setOnClickHistory() {
        bundle.putString(EducationIntentKey.FLAGACTIVITY, EducationIntentKey.HISTORY);
        intent.putExtra("mine", bundle);//需修改统一字段
        intent.setClass(context, HistoryActivity.class);
        startActivity(intent);
    }

    //意见反馈
    @OnClick(R.id.my_feedback_card)
    public void setOnClickFeedback() {
        if (myInfo == null || myInfo.getUserid() == 0) {
            EducationUrl.showToast(context, "尚未登录");
            intent.setClass(context, LoginActivity.class);
            startActivityForResult(intent,1);
        }else {
            intent.setClass(context, FeedBackActivity.class);
            startActivity(intent);
        }
    }

    @OnClick(R.id.logon_button)
    public void setOnClickLogonButton() {
        intent.setClass(context, LoginActivity.class);
        startActivityForResult(intent, EducationIntentKey.LOGONCODE);

    }

    @OnClick(R.id.register_button)
    public void setOnClickRegisterButton() {
        intent.setClass(context, RegisterActivity.class);
        bundle.putSerializable("myinfo", myInfo);
        bundle.putString(EducationIntentKey.FLAG,"register");
        intent.putExtra("minefragment", bundle);//需修改统一字段
        startActivityForResult(intent, EducationIntentKey.REGISTERCODE);
    }

    @OnClick(R.id.my_setting_card)
    public void setOnClickSettingCard() {

        intent.setClass(context, SettingActivity.class);
        startActivity(intent);

    }

    @OnClick(R.id.my_selftitle_card)
    public void setOnClickMySelf() {

        intent.setClass(context, MySelfActivity.class);
        startActivity(intent);

    }
    @OnClick(R.id.my_changepassword_card)
    public void setOnClickMyChangePassword() {
        intent.setClass(context, RegisterActivity.class);
        bundle.putSerializable("myinfo", myInfo);
        bundle.putString(EducationIntentKey.FLAG, "change");
        intent.putExtra("minefragment", bundle);//需修改统一字段
        startActivity(intent);
    }
    @OnClick(R.id.check_update)
    public void checkUpdate(){
        CheckUpdate checkUpdate = new CheckUpdate(context);
        checkUpdate.checkUp();
    }
}
